<?php

namespace app\admin\controller\data;

use app\common\controller\Backend;
use app\common\model\coll\Collection;
use app\common\model\factory\Sales;
use app\common\model\purchase\Allot;
use app\common\model\purchase\Enter;
use app\common\model\sales\Checkout;
use app\web\controller\Factory;
use think\Db;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Voucher extends Backend
{
    
    /**
     * Voucher模型对象
     * @var \app\common\model\data\Voucher
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\data\Voucher;

    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            $filter = json_decode($this->request->get('filter'),true);
            $op = json_decode($this->request->get('op'),true);
//            dump($filter);
//            exit;
            if(isset($filter['number'])){
                $rest = substr(trim($filter['number']), 0,2);
                $source_id = 0;
                $type = 0;
                if($rest == 'TD'){
                    $model = new Allot;
                    $row = $model->where(array('number'=>trim($filter['number'])))->find();
                    if($row){
                        $source_id = $row['id'];
                    }
                    $type=1;
                }
                if($rest == 'XK'){
                    $model = new Checkout();
                    $row = $model->where(array('number'=>trim($filter['number'])))->find();
                    if($row){
                        $source_id = $row['id'];
                    }
                    $type=2;
                }
                if($rest == 'JD'){//采购入库
                    $model = new Enter();
                    $row = $model->where(array('number'=>trim($filter['number'])))->find();
                    if($row){
                        $source_id = $row['id'];
                    }
                    $type=3;
                }
                if($rest == 'XS'){
                    $model = new Sales();
                    $row = $model->where(array('number'=>trim($filter['number'])))->find();
                    if($row){
                        $source_id = $row['id'];
                    }
                    $type=4;
                }
                if($rest == 'XS'){
                    $model = new Collection();
                    $row = $model->where(array('number'=>trim($filter['number'])))->find();
                    if($row){
                        $source_id = $row['id'];
                    }
                    $type=17;
                }
               if(empty($source_id)){
                   return array("total" => 0, "rows" => []);
               }
               unset($filter['number']);
               $filter['source_id'] = $source_id;
//               $filter['type'] = $type;
//                $this->request->get(["filter"=> json_encode($filter),'op'=>json_encode($op)]);
            }

            $this->request->get(["filter"=> json_encode($filter),'op'=>json_encode($op)]);
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                    ->where($where)
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    ->where($where)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();

            foreach ($list as $row) {
                $row->visible(['id','image','remark','type','createtime','is_go','source_id']);
            }
            $list = collection($list)->toArray();
            $types = dill_type();
            $gos = $this->model->is_go_status();
            foreach ($list as $k=> $v){
                if($v['type'] == 5){
                    $model = new Allot;
                }
                if($v['type'] == 7){
                    $model = new Checkout();
                }
                if($v['type'] == 2){
                    $model = new Enter();
                }
                if($v['type'] == 12){
                    $model = new Sales();
                }
                if($v['type'] == 17){
                    $model = new Collection();
                }
                $row = $model->where(array('id'=>$v['source_id']))->find();
                $list[$k]['number'] = $row['number'];
                $list[$k]['is_go'] = $gos[$v['is_go']];
                $list[$k]['type'] = $types[$v['type']];
            }
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }
}
